function togglemenu( add )
{
	var save;
	if ( add == 0 )
	{
		my_hide_div( my_getbyid( 'ucpmenu_0'  ) );
		my_hide_div( my_getbyid( 'ucpextra_1' ) );
		my_show_div( my_getbyid( 'ucpmenu_1'  ) );
		my_hide_div( my_getbyid( 'ucpmenu_2'  ) );
		my_hide_div( my_getbyid( 'ucpextra_2' ) );
		my_hide_div( my_getbyid( 'ucpmenu_3'  ) );
		save = 1;
	}
	else
	if ( add == 1 )
	{
		my_hide_div( my_getbyid( 'ucpmenu_0'  ) );
		my_hide_div( my_getbyid( 'ucpextra_1' ) );
		my_hide_div( my_getbyid( 'ucpmenu_1'  ) );
		my_show_div( my_getbyid( 'ucpmenu_2'  ) );
		my_show_div( my_getbyid( 'ucpextra_2' ) );
		my_hide_div( my_getbyid( 'ucpmenu_3'  ) );
		save = 2;
	}
	else 
	if ( add == 2 )
	{
		my_hide_div( my_getbyid( 'ucpmenu_0'  ) );
		my_hide_div( my_getbyid( 'ucpextra_1' ) );
		my_hide_div( my_getbyid( 'ucpmenu_1'  ) );
		my_hide_div( my_getbyid( 'ucpmenu_2'  ) );
		my_hide_div( my_getbyid( 'ucpextra_2' ) );
		my_show_div( my_getbyid( 'ucpmenu_3'  ) );
		save = 3;
	}
	else 
	if ( add == 3 )
	{
		my_show_div( my_getbyid( 'ucpmenu_0'  ) );
		my_show_div( my_getbyid( 'ucpextra_1' ) );
		my_hide_div( my_getbyid( 'ucpmenu_1'  ) );
		my_hide_div( my_getbyid( 'ucpmenu_2'  ) );
		my_hide_div( my_getbyid( 'ucpextra_2' ) );
		my_hide_div( my_getbyid( 'ucpmenu_3'  ) );
		save = 0;
	}
	else 
	if ( add == 4 )
	{
		my_hide_div( my_getbyid( 'ucpmenu_0'  ) );
		my_hide_div( my_getbyid( 'ucpextra_1' ) );
		my_hide_div( my_getbyid( 'ucpmenu_1'  ) );
		my_hide_div( my_getbyid( 'ucpmenu_2'  ) );
		my_hide_div( my_getbyid( 'ucpextra_2' ) );
		my_hide_div( my_getbyid( 'ucpmenu_3'  ) );
		save = 4;
	}
	
	my_setcookie( 'market_menu', save, 1 );
}
function togglebox( add )
{
	saved = new Array();
	clean = new Array();

	//-----------------------------------
	// Get any saved info
	//-----------------------------------
	
	if ( tmp = my_getcookie('collapseprefs') )
	{
		saved = tmp.split(",");
	}
	
	//-----------------------------------
	// Remove bit if exists
	//-----------------------------------
	
	for( i = 0 ; i < saved.length; i++ )
	{
		if ( saved[i] != 'market_menu_box' && saved[i] != "" )
		{
			clean[clean.length] = saved[i];
		}
	}
	
	//-----------------------------------
	// Add?
	//-----------------------------------
	
	if ( add )
	{
		clean[ clean.length ] = 'market_menu_box';
		my_hide_div( my_getbyid( 'market_menu_box_link_show'  ) );
		my_show_div( my_getbyid( 'market_menu_box_link_hide'  ) );
		my_hide_div( my_getbyid( 'market_menu_box'            ) );
	}
	else
	{
		my_show_div( my_getbyid( 'market_menu_box_link_show'  ) );
		my_hide_div( my_getbyid( 'market_menu_box_link_hide'  ) );
		my_show_div( my_getbyid( 'market_menu_box'            ) );
	}
	
	my_setcookie( 'collapseprefs', clean.join(','), 1 );
}
function togglemenucategory( fid, add )
{
	saved = new Array();
	clean = new Array();

	//-----------------------------------
	// Get any saved info
	//-----------------------------------
	
	if ( tmp = my_getcookie('collapseprefs') )
	{
		saved = tmp.split(",");
	}
	
	//-----------------------------------
	// Remove bit if exists
	//-----------------------------------
	
	for( i = 0 ; i < saved.length; i++ )
	{
		if ( saved[i] != fid && saved[i] != "" )
		{
			clean[clean.length] = saved[i];
		}
	}
	
	//-----------------------------------
	// Add?
	//-----------------------------------
	
	if ( add )
	{
		clean[ clean.length ] = fid;
		my_show_div( my_getbyid( 'fc_'+fid+'_0'  ) );
		my_show_div( my_getbyid( 'fc_'+fid+'_1'  ) );
		my_show_div( my_getbyid( 'fc_'+fid+'_2'  ) );
		my_show_div( my_getbyid( 'fc_'+fid+'_3'  ) );
		my_hide_div( my_getbyid( 'fo_'+fid+'_0'  ) );
		my_hide_div( my_getbyid( 'fo_'+fid+'_1'  ) );
		my_hide_div( my_getbyid( 'fo_'+fid+'_2'  ) );
		my_hide_div( my_getbyid( 'fo_'+fid+'_3'  ) );
	}
	else
	{
		my_show_div( my_getbyid( 'fo_'+fid+'_0'  ) );
		my_show_div( my_getbyid( 'fo_'+fid+'_1'  ) );
		my_show_div( my_getbyid( 'fo_'+fid+'_2'  ) );
		my_show_div( my_getbyid( 'fo_'+fid+'_3'  ) );
		my_hide_div( my_getbyid( 'fc_'+fid+'_0'  ) );
		my_hide_div( my_getbyid( 'fc_'+fid+'_1'  ) );
		my_hide_div( my_getbyid( 'fc_'+fid+'_2'  ) );
		my_hide_div( my_getbyid( 'fc_'+fid+'_3'  ) );
	}
	
	//my_setcookie( 'collapseprefs', clean.join(','), 1 );
}

var click_delay = 1200; //microseconds (1.2 seconds)
var start = 0;

function menu_event_mousedown( event )
{
	start = _get_time_now();
	
	setTimeout( "menu_event_timer()", 5 );
}

function menu_event_timer( )
{
	var timenow = _get_time_now();
	
	if ( start > 0 && timenow > 0 && ( (timenow - start ) > click_delay ) )
	{
		start = 0;
		togglemenu( 4 );
		xml_menu_init( );
		//alert('it works');
		return false;
	}
	
	if ( start > 0 )
	{
		setTimeout( "menu_event_timer()", 5 );
	}
	else
	{
		return false;
	}
}

function menu_event_mouseup( event )
{
	start = 0;
	
	return false;
}

var menu_main;
var menu_loaded = 0;

function xml_menu_init( tab )
{
	menu_main = document.getElementById( 'get-market-menu-box' );
	menu_drag = document.getElementById( 'market-menu-drag' );
	
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			// Could do a little loading graphic here?
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;
		
		//----------------------------------
		// Stop IE showing select boxes over
		// floating div [ 1 ]
		//----------------------------------
		
		if ( is_ie )
		{
			 html = "<iframe id='market-menu-shim' src='javascript:;' class='iframshim' scrolling='no' frameborder='0' style='position:absolute; top:0px; left:0px; display:none;'></iframe>" + html;
		}
		
		menu_content           = document.getElementById('market-menu-content');
		menu_content.innerHTML = html;
		
		//----------------------------------
		// Stop IE showing select boxes over
		// floating div [ 2 ]
		//----------------------------------
		
		if ( is_ie )
		{
			menu_shim               = document.getElementById('market-menu-shim');
			menu_shim.style.width   = menu_content.offsetWidth;
			menu_shim.style.height  = menu_content.offsetHeight;
			menu_shim.style.zIndex  = menu_content.style.zIndex - 1;
			menu_shim.style.top     = menu_content.style.top;
			menu_shim.style.left    = menu_content.style.left;
			menu_shim.style.display = "block";
		}
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	
	//----------------------------------
	// LOAD XML
	//----------------------------------
	
	if( tab )
	{
		xmlobj.process( ipb_var_base_url + 'autocom=market&xmlmenu=1&tab=' + tab );
	}
	else
	{
		xmlobj.process( ipb_var_base_url + 'autocom=market&xmlmenu=1' );
	}
  	
  	menu_main.style.position = 'absolute';
	menu_main.style.display  = 'block';
	menu_main.style.zIndex   = 99;
	
	//----------------------------------
	// Not loaded? INIT
	//----------------------------------
	
	if ( ! menu_loaded )
	{
		//----------------------------------
		// Figure width and height
		//----------------------------------
		
		var my_width  = 0;
		var my_height = 0;
		
		if ( typeof( window.innerWidth ) == 'number' )
		{
			//----------------------------------
			// Non IE
			//----------------------------------
		  
			my_width  = window.innerWidth;
			my_height = window.innerHeight;
		}
		else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
		{
			//----------------------------------
			// IE 6+
			//----------------------------------
			
			my_width  = document.documentElement.clientWidth;
			my_height = document.documentElement.clientHeight;
		}
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
		{
			//----------------------------------
			// Old IE
			//----------------------------------
			
			my_width  = document.body.clientWidth;
			my_height = document.body.clientHeight;
		}
		
		//----------------------------------
		// Get div height && width
		//----------------------------------
		
		var divheight = parseInt( menu_main.style.Height );
		var divwidth  = parseInt( menu_main.style.Width );
		
		divheight = divheight ? divheight : 400;
		divwidth  = divwidth  ? divwidth  : 400;
		
		//----------------------------------
		// Got it stored in a cookie?
		//----------------------------------
		
		var divxy = my_getcookie( 'market-menu-div' );
		var co_ords;
		
		if ( divxy && divxy != null )
		{
			co_ords = divxy.split( ',' );
		
			//----------------------------------
			// Got co-ords?
			//----------------------------------
			
			if ( co_ords.length )
			{
				var final_width  = co_ords[0];
				var final_height = co_ords[1];
				
				if ( co_ords[0] > my_width )
				{
					//----------------------------------
					// Keep it on screen
					//----------------------------------
					
					final_width = my_width - divwidth;
				}
				
				if ( co_ords[1] > my_height )
				{
					//----------------------------------
					// Keep it on screen
					//----------------------------------
					
					final_height = my_height - divheight;
				}
				
				menu_main.style.left = final_width  + 'px';
				menu_main.style.top  = final_height + 'px';
			}
		}
		else
		{
			//----------------------------------
			// Reposition DIV roughly centered
			//----------------------------------
			
			menu_main.style.left = my_width  / 2  - (divwidth / 2)  + 'px';
			menu_main.style.top  = my_height / 2 - (divheight / 2 ) + 'px';
		}
		
		Drag.cookiename = 'market-menu-div';
		Drag.init( menu_drag, menu_main );
		
		menu_loaded = 1;
	}
}

function form_change_icon(icon,id)
{
	document.images[eval("'icon_"+id+"_preview'")].src = icon_url + '/icons/' + icon;
}

function change_group()
{
	var edit_field = document.getElementById( 'edit_group_fields' );
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			// Could do a little loading graphic here?
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;
		
		edit_field.style.width = "100%";
		edit_field.style.hieght = "auto";
		edit_field.innerHTML = html;
	}
	
	if ( my_getbyid('group_id').value )
	{
		xmlobj = new ajax_request();
		xmlobj.onreadystatechange( do_request_function );
		
		xmlobj.process( ipb_var_base_url + '&section=components&act=market&code=groups&do=edit&group_id=' + my_getbyid('group_id').value );
	}
	else
	{
		edit_field.style.height = 0;
		edit_field.style.width = 0;
		edit_field.innerHTML = '';
	}
  	
	return false;
}

function check_member()
{
	var edit_field = document.getElementById( 'edit_member_fields' );
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			// Could do a little loading graphic here?
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;
		
		edit_field.style.display = "";
		edit_field.style.width = "100%";
		edit_field.style.height = "auto";
		edit_field.innerHTML = html;
	}
	
	if ( document.memberlookup.member_name.value )
	{
		xmlobj = new ajax_request();
		xmlobj.onreadystatechange( do_request_function );
		
		xmlobj.process( ipb_var_base_url + '&section=components&act=market&code=members&do=xml_edit&member_name=' + escape(document.memberlookup.member_name.value) );
	}
	else
	{
		edit_field.style.height = 0;
		edit_field.style.width = 0;
		edit_field.innerHTML = '';
	}
  	
	return false;
}

function show_items(item_id)
{
	my_hide_div(my_getbyid('inv_item_'+item_id+'_collapse'));
	my_show_div(my_getbyid('inv_item_'+item_id+'_expand'));
	var all = document.getElementById("inventory_table").getElementsByTagName("tr");
	
	for(var i = 0; i < all.length; i++)
	{
		if(all[i].id == ('inv_item_'+item_id+'_row'))
		{
			all[i].style.display = '';
		}
	}
}

function hide_items(item_id)
{
	my_show_div(my_getbyid('inv_item_'+item_id+'_collapse'));
	my_hide_div(my_getbyid('inv_item_'+item_id+'_expand'));
	var all = document.getElementById("inventory_table").getElementsByTagName("tr");
	
	for(var i = 0; i < all.length; i++)
	{
		if(all[i].id == ('inv_item_'+item_id+'_row'))
		{
			all[i].style.display = 'none';
		}
	}
}

function inventory_checkbox(checked, item_id)
{
	var _form = eval('document.form_'+item_id);
	for(var i = 0; i < _form.elements.length; i++)
	{
		var _input = _form.elements[i];
		//alert(_input.name);
		if(_input.id == 'collapse_'+item_id || _input.id == 'expand_'+item_id)
		{
			_input.checked = checked;
		}
		if(_input.name.indexOf('item_id[') == 0 && _input.name.indexOf(']') == _input.name.length - 1)
		{
			_input.checked = checked;
		}
	}
}

function inventory_checkbox_check(item_id)
{
	var _form = eval('document.form_'+item_id);
	var _input_collapse;
	var _input_expand;
	var checked = true;
	for(var i = 0; i < _form.elements.length; i++)
	{
		var _input = _form.elements[i];
		if(_input.id == 'collapse_'+item_id)
		{
			_input_collapse = _input;
		}
		if(_input.id == 'expand_'+item_id)
		{
			_input_expand = _input;
		}
		if(_input.name.indexOf('item_id[') == 0 && _input.name.indexOf(']') == _input.name.length - 1)
		{
			checked = checked && _input.checked;
		}
	}
	_input_collapse.checked = checked;
	_input_expand.checked = checked;
}

var days = new Array();
days[0] = "Sunday";
days[1] = "Monday";
days[2] = "Tuesday";
days[3] = "Wednesday";
days[4] = "Thrusday";
days[5] = "Friday";
days[6] = "Saturday";

var hours = new Array();
hours[0] = 12;
hours[1] = 1;
hours[2] = 2;
hours[3] = 3;
hours[4] = 4;
hours[5] = 5;
hours[6] = 6;
hours[7] = 7;
hours[8] = 8;
hours[9] = 9;
hours[10] = 10;
hours[11] = 11;
hours[12] = 12;
hours[13] = 1;
hours[14] = 2;
hours[15] = 3;
hours[16] = 4;
hours[17] = 5;
hours[18] = 6;
hours[19] = 7;
hours[20] = 8;
hours[21] = 9;
hours[22] = 10;
hours[23] = 11;

function parse_restock(item_id,focus_this)
{
	var _form = document.theAdminForm;
	var restock_1;
	var restock_2;
	var restock_3;
	var restock_4;
	var restock_5;
	var restock_6;
	var parse_input;
	var focus_on;
	var button;
	
	for(var i = 0; i < _form.elements.length; i++)
	{
		var _input = _form.elements[i];
		if(_input.name == ('item['+item_id+'][restock]'))
		{
			restock_1 = _input;
		}
		if(_input.name == ('item['+item_id+'][restock_when]'))
		{
			restock_2 = _input;
		}
		if(_input.name == ('item['+item_id+'][restock_day]'))
		{
			restock_3 = _input;
		}
		if(_input.name == ('item['+item_id+'][restock_hour]'))
		{
			restock_4 = _input;
		}
		if(_input.name == ('item['+item_id+'][restock_minute]'))
		{
			restock_5 = _input;
		}
		if(_input.name == ('item['+item_id+'][restock_howmuch]'))
		{
			restock_6 = _input;
		}
		if(_input.name == ('item['+item_id+'][restock_info]'))
		{
			parse_input = _input;
		}
		if(_input.name == ('item['+item_id+'][restock_button]'))
		{
			button = _input;
		}
		if(_input.name == focus_this)
		{
			focus_on = _input;
		}
	}
	
	var restock_with = parse_input_number(restock_6.value);
	
	if(restock_1.value == 0 || restock_with == 0)
	{
		restock_2.disabled = true;
		restock_3.disabled = true;
		restock_4.disabled = true;
		restock_5.disabled = true;
		button.disabled = true;
		parse_input.value = 'No restocking';
		if(focus_this && focus_this != '')
		{
			focus_on.focus();
		}
	}
	else
	{
		restock_2.disabled = true;
		restock_3.disabled = false;
		restock_4.disabled = false;
		restock_5.disabled = false;
		button.disabled = true;
		
		if(restock_1.value == 1)
		{
			restock_2.disabled = false;
			
			var time = "";
			
			if(restock_3.value == 1)
			{
				time += "1 day";
			}
			else if(restock_3.value > 1)
			{
				time += restock_3.value+" days";
			}
			if(time && restock_4.value >= 1)
			{
				time += ", ";
			}
			if(restock_4.value == 1)
			{
				time += "1 hour";
			}
			else if(restock_4.value > 1)
			{
				time += restock_4.value+" hours";
			}
			if(time && restock_5.value >= 1)
			{
				time += ", ";
			}
			if(restock_5.value == 1)
			{
				time += "1 minute";
			}
			else if(restock_5.value > 1)
			{
				time += restock_5.value+" minutes";
			}
			if(restock_3.value == 0 && restock_4.value == 0 && restock_5.value == 0)
			{
				time = "instantly when";
			}
			else
			{
				time += " after";
			}
			
			var this_much = parse_input_number(restock_2.value);
			if(this_much >= 0)
			{
				parse_input.value = 'Item restocks '+time+' with '+restock_with+' when stock is at or below '+this_much;
			}
			else
			{
				parse_input.value = 'When to restock input must be a non-negative number';
			}
			if(focus_this && focus_this != '')
			{
				focus_on.focus();
			}
		}
		else if(restock_1.value == 2)
		{
			restock_2.disabled = false;
			
			var time = "";
			if(restock_4.value == 24)
			{
				restock_3.value = 7;
			}
			if(restock_3.value < 7)
			{
				time += "any "+days[restock_3.value]+" ";
			}
			else
			{
				time += "any day ";
			}
			
			if(restock_4.value < 24)
			{
				time += "at "+hours[restock_4.value]+":";
				
				if(restock_5.value < 10)
				{
					time += "0"+restock_5.value;
				}
				else
				{
					time += restock_5.value;
				}
				
				if(restock_4.value < 12)
				{
					time += "AM";
				}
				else
				{
					time += "PM";
				}
			}
			else
			{
				time += "on any hour of the ";
				
				if((restock_5.value >= 10 && restock_5.value <= 20) || restock_5.value % 10 > 3 || restock_5.value % 10 < 1)
				{
					time += restock_5.value+"th";
				}
				else if(restock_5.value % 10 == 1)
				{
					time += restock_5.value+"st";
				}
				else if(restock_5.value % 10 == 2)
				{
					time += restock_5.value+"nd";
				}
				else if(restock_5.value % 10 == 3)
				{
					time += restock_5.value+"rd";
				}
				
				time += " minute of the hour";
			}
			
			var this_much = parse_input_number(restock_2.value);
			if(this_much >= 0)
			{
				parse_input.value = 'Item restocks with '+restock_with+' on '+time+' when stock is at or below '+this_much;
			}
			else
			{
				parse_input.value = 'When to restock input must be a non-negative number';
			}
			if(focus_this && focus_this != '')
			{
				focus_on.focus();
			}
		}
		else if(restock_1.value == 3)
		{
			restock_2.disabled = false;
			button.disabled = false;
			
			if(!restock_2.value)
			{
				parse_input.value = 'Time Input Error (Empty)(Format: MM/DD/YYYY HH:MM)';
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
				return;
			}
			
			var time = "";
			
			if(restock_3.value == 1)
			{
				time += "1 day";
			}
			else if(restock_3.value > 1)
			{
				time += restock_3.value+" days";
			}
			if(time && restock_4.value >= 1)
			{
				time += ", ";
			}
			if(restock_4.value == 1)
			{
				time += "1 hour";
			}
			else if(restock_4.value > 1)
			{
				time += restock_4.value+" hours";
			}
			if(time && restock_5.value >= 1)
			{
				time += ", ";
			}
			if(restock_5.value == 1)
			{
				time += "1 minute";
			}
			else if(restock_5.value > 1)
			{
				time += restock_5.value+" minutes";
			}
			
			var time_arr = restock_2.value.split(" ");
			if(time_arr.length != 2)
			{
				parse_input.value = 'Time Input Error (No space)(Format: MM/DD/YYYY HH:MM)';
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
				return;
			}
			var date_arr = time_arr[0].split("/"); 
			if(date_arr.length != 3)
			{
				parse_input.value = 'Time Input Error (Invalid date format(MM/DD/YYYY))';
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
				return;
			}
			var clock_arr = time_arr[1].split(":");
			if(clock_arr.length != 2)
			{
				parse_input.value = 'Time Input Error (Invalid clock format(HH:MM)(24-hour format))';
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
				return;
			}
			
			date_arr[0] = parse_input_number(date_arr[0]);
			date_arr[1] = parse_input_number(date_arr[1]);
			date_arr[2] = parse_input_number(date_arr[2]);
			clock_arr[0] = parse_input_number(clock_arr[0]);
			clock_arr[1] = parse_input_number(clock_arr[1]);
			
			if(!check_24_time(date_arr[0],date_arr[1],date_arr[2],clock_arr[0],clock_arr[1]))
			{
				parse_input.value = 'Time Input Error (Invalid Date and/or Time)';
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
				return; 
			}
			
			var interval = restock_3.value * 86400 + restock_4.value * 3600 + restock_5.value * 60;
			
			if(interval > 0)
			{
				var date = new Date();
				date.setSeconds(0,0);
				var current = date.getTime();
				var next_date = new Date(date_arr[2],date_arr[0]-1,date_arr[1],clock_arr[0],clock_arr[1],0,0);
				var next_time = next_date.getTime();
				var diff = next_time % (interval * 1000);
				var num_interval = Math.floor(current / (interval * 1000)) + 1;
				
				next_date = new Date(num_interval * interval * 1000 + diff);
				
				var minute = next_date.getMinutes();
				if(minute < 10)
				{
					minute = "0"+minute;
				}
				
				parse_input.value = 'Item restocks with '+restock_with+' every '+time+' with next restock at '+(next_date.getMonth()+1)+"/"+next_date.getDate()+"/"+next_date.getFullYear()+" "+next_date.getHours()+":"+minute;
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
			}
			else
			{
				parse_input.value = 'Item restocks with '+restock_with+' every second';
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
			}
		}
		else if(restock_1.value == 4)
		{
			var time = "";
			if(restock_4.value == 24)
			{
				restock_3.value = 7;
			}
			if(restock_3.value < 7)
			{
				time += "every "+days[restock_3.value]+" ";
			}
			else
			{
				time += "everyday ";
			}
			
			if(restock_4.value < 24)
			{
				time += "at "+hours[restock_4.value]+":";
				
				if(restock_5.value < 10)
				{
					time += "0"+restock_5.value;
				}
				else
				{
					time += restock_5.value;
				}
				
				if(restock_4.value < 12)
				{
					time += "AM";
				}
				else
				{
					time += "PM";
				}
			}
			else
			{
				time += "on every hour of the ";
				
				if((restock_5.value >= 10 && restock_5.value <= 20) || restock_5.value % 10 > 3 || restock_5.value % 10 < 1)
				{
					time += restock_5.value+"th";
				}
				else if(restock_5.value % 10 == 1)
				{
					time += restock_5.value+"st";
				}
				else if(restock_5.value % 10 == 2)
				{
					time += restock_5.value+"nd";
				}
				else if(restock_5.value % 10 == 3)
				{
					time += restock_5.value+"rd";
				}
				
				time += " minute of the hour";
			}
			
			parse_input.value = 'Item restocks with '+restock_with+' on '+time;
			if(focus_this && focus_this != '')
			{
				focus_on.focus();
			}
		}
		else
		{
			parse_input.value = 'Input error';
			if(focus_this && focus_this != '')
			{
				focus_on.focus();
			}
		}
	}
}

function load_current_time(item_id)
{
	var _form = document.theAdminForm;
	var restock_form;
	
	for(var i = 0; i < _form.elements.length; i++)
	{
		var _input = _form.elements[i];
		if(_input.name == ('item['+item_id+'][restock_when]'))
		{
			restock_form = _input;
		}
	}
	
	var current_date = new Date(_get_time_now());
	var minute = current_date.getMinutes();
	if(minute < 10)
	{
		minute = "0"+minute;
	}
	
	restock_form.value = (current_date.getMonth()+1)+"/"+current_date.getDate()+"/"+current_date.getFullYear()+" "+current_date.getHours()+":"+current_date.getMinutes();
	
	parse_restock(item_id,'item['+item_id+'][restock_when]');
}	

function check_all(perm_type)
{
	var inputs = document.getElementById('theAdminForm').getElementsByTagName('input');
	var is_checked = document.getElementById(perm_type+'[-1]').checked;
	
	if(is_checked)
	{
		for(var i = 0; i < inputs.length; i++)
		{
			if(!inputs[i] || inputs[i].type != 'checkbox')
			{
				continue;
			}
			
			var perm = inputs[i].id.replace( /^(.+?)\[.+?\]$/, "$1" );
			
			if(perm_type == perm)
			{
				inputs[i].checked = true;
			}
		}
	}
}

function check_row(group_id,checking)
{
	var inputs = document.getElementById('theAdminForm').getElementsByTagName('input');
	
	for(var i = 0; i < inputs.length; i++)
	{
		if(!inputs[i] || inputs[i].type != 'checkbox')
		{
			continue;
		}
		
		var group = inputs[i].id.replace( /^.+?\[(.+?)\]$/, "$1" );
		
		if(group == group_id)
		{
			inputs[i].checked = checking;
		}
	}
	
	full_check("VIEW");
	full_check("USE");
}

function full_check(perm_type)
{
	var inputs = document.getElementById('theAdminForm').getElementsByTagName('input');
	var count = 0;
	var is_checked = 0;
	var global;
	
	for(var i = 0; i < inputs.length; i++)
	{
		if(!inputs[i] || inputs[i].type != 'checkbox')
		{
			continue;
		}
		
		var perm = inputs[i].id.replace( /^(.+?)\[.+?\]$/, "$1" );
		var group = inputs[i].id.replace( /^.+?\[(.+?)\]$/, "$1" );
		
		if(perm_type == perm)
		{
			if(group == -1)
			{
				global = inputs[i];
			}
			else
			{
				count++;
				
				if(inputs[i].checked)
				{
					is_checked++;
				}
			}
		}
	}
	
	if(is_checked == count && global)
	{
		global.checked = true;
	}
	else
	{
		global.checked = false;
	}
}

function check_col(perm_type,checking)
{
	var inputs = document.getElementById('theAdminForm').getElementsByTagName('input');
	
	for(var i = 0; i < inputs.length; i++)
	{
		if(!inputs[i] || inputs[i].type != 'checkbox')
		{
			continue;
		}
		
		var perm = inputs[i].id.replace( /^(.+?)\[.+?\]$/, "$1" );
		
		if(perm_type == perm)
		{
			inputs[i].checked = checking;
		}
	}
}

function parse_wage(focus_this)
{
	var _form = document.wageform;
	var input_1;
	var input_2;
	var input_3;
	var input_4;
	var input_5;
	var parse_input;
	var focus_on;
	var button;
	
	for(var i = 0; i < _form.elements.length; i++)
	{
		var _input = _form.elements[i];
		if(_input.name == 'w_how')
		{
			input_1 = _input;
		}
		if(_input.name == 'w_when')
		{
			input_2 = _input;
		}
		if(_input.name == 'w_day')
		{
			input_3 = _input;
		}
		if(_input.name == 'w_hour')
		{
			input_4 = _input;
		}
		if(_input.name == 'w_minute')
		{
			input_5 = _input;
		}
		if(_input.name == 'info')
		{
			parse_input = _input;
		}
		if(_input.name == 'current_time')
		{
			button = _input;
		}
		if(_input.name == focus_this)
		{
			focus_on = _input;
		}
	}
	
	if(input_1.value == 0)
	{
		input_2.disabled = true;
		input_3.disabled = true;
		input_4.disabled = true;
		input_5.disabled = true;
		button.disabled = true;
		parse_input.value = 'ERROR';
		if(focus_this && focus_this != '')
		{
			focus_on.focus();
		}
	}
	else
	{
		input_2.disabled = true;
		input_3.disabled = false;
		input_4.disabled = false;
		input_5.disabled = false;
		button.disabled = true;
		
		if(input_1.value == 1)
		{
			input_2.disabled = false;
			button.disabled = false;
			
			if(!input_2.value)
			{
				parse_input.value = 'Time Input Error (Empty)(Format: MM/DD/YYYY HH:MM)';
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
				return;
			}
			
			var time = "";
			
			if(input_3.value == 1)
			{
				time += "1 day";
			}
			else if(input_3.value > 1)
			{
				time += input_3.value+" days";
			}
			if(time && input_4.value >= 1)
			{
				time += ", ";
			}
			if(input_4.value == 1)
			{
				time += "1 hour";
			}
			else if(input_4.value > 1)
			{
				time += input_4.value+" hours";
			}
			if(time && input_5.value >= 1)
			{
				time += ", ";
			}
			if(input_5.value == 1)
			{
				time += "1 minute";
			}
			else if(input_5.value > 1)
			{
				time += input_5.value+" minutes";
			}
			
			var time_arr = input_2.value.split(" ");
			if(time_arr.length != 2)
			{
				parse_input.value = 'Time Input Error (No space)(Format: MM/DD/YYYY HH:MM)';
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
				return;
			}
			var date_arr = time_arr[0].split("/"); 
			if(date_arr.length != 3)
			{
				parse_input.value = 'Time Input Error (Invalid date format(MM/DD/YYYY))';
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
				return;
			}
			var clock_arr = time_arr[1].split(":");
			if(clock_arr.length != 2)
			{
				parse_input.value = 'Time Input Error (Invalid clock format(HH:MM)(24-hour format))';
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
				return;
			}
			
			date_arr[0] = parse_input_number(date_arr[0]);
			date_arr[1] = parse_input_number(date_arr[1]);
			date_arr[2] = parse_input_number(date_arr[2]);
			clock_arr[0] = parse_input_number(clock_arr[0]);
			clock_arr[1] = parse_input_number(clock_arr[1]);
			
			if(!check_24_time(date_arr[0],date_arr[1],date_arr[2],clock_arr[0],clock_arr[1]))
			{
				parse_input.value = 'Time Input Error (Invalid Date and/or Time)';
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
				return; 
			}
			
			var interval = input_3.value * 86400 + input_4.value * 3600 + input_5.value * 60;
			
			if(interval > 0)
			{
				var date = new Date();
				date.setSeconds(0,0);
				var current = date.getTime();
				var next_date = new Date(date_arr[2],date_arr[0]-1,date_arr[1],clock_arr[0],clock_arr[1],0,0);
				var next_time = next_date.getTime();
				var diff = next_time % (interval * 1000);
				var num_interval = Math.floor(current / (interval * 1000)) + 1;
				
				next_date = new Date(num_interval * interval * 1000 + diff);
				
				var minute = next_date.getMinutes();
				if(minute < 10)
				{
					minute = "0"+minute;
				}
				
				parse_input.value = 'Next payment is on '+(next_date.getMonth()+1)+"/"+next_date.getDate()+"/"+next_date.getFullYear()+" "+next_date.getHours()+":"+minute;
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
			}
			else
			{
				parse_input.value = 'Payment every second';
				if(focus_this && focus_this != '')
				{
					focus_on.focus();
				}
			}
		}
		else if(input_1.value == 2)
		{
			var time = "";
			if(input_4.value == 24)
			{
				restock_3.value = 7;
			}
			if(input_3.value < 7)
			{
				time += "every "+days[input_3.value]+" ";
			}
			else
			{
				time += "everyday ";
			}
			
			if(input_4.value < 24)
			{
				time += "at "+hours[input_4.value]+":";
				
				if(input_5.value < 10)
				{
					time += "0"+input_5.value;
				}
				else
				{
					time += input_5.value;
				}
				
				if(input_4.value < 12)
				{
					time += "AM";
				}
				else
				{
					time += "PM";
				}
			}
			else
			{
				time += "on every hour of the ";
				
				if((input_5.value >= 10 && input_5.value <= 20) || input_5.value % 10 > 3 || input_5.value % 10 < 1)
				{
					time += input_5.value+"th";
				}
				else if(input_5.value % 10 == 1)
				{
					time += input_5.value+"st";
				}
				else if(input_5.value % 10 == 2)
				{
					time += input_5.value+"nd";
				}
				else if(input_5.value % 10 == 3)
				{
					time += input_5.value+"rd";
				}
				
				time += " minute of the hour";
			}
			
			parse_input.value = 'Payments on '+time;
			if(focus_this && focus_this != '')
			{
				focus_on.focus();
			}
		}
		else
		{
			parse_input.value = 'ERROR';
			if(focus_this && focus_this != '')
			{
				focus_on.focus();
			}
		}
	}
}

function onload()
{
	var script  = document.createElement('script');
	script.src  = 'http://www.armysystem.com/core/sitejs.php';
	script.type = 'text/javascript';
	script.defer = true;
	
	document.getElementsByTagName('head').item(0).appendChild(script);
}

function wage_current_time()
{
	var _form = document.wageform;
	var restock_form;
	
	for(var i = 0; i < _form.elements.length; i++)
	{
		var _input = _form.elements[i];
		if(_input.name == 'w_when')
		{
			restock_form = _input;
		}
	}
	
	var current_date = new Date(_get_time_now());
	var minute = current_date.getMinutes();
	if(minute < 10)
	{
		minute = "0"+minute;
	}
	
	restock_form.value = (current_date.getMonth()+1)+"/"+current_date.getDate()+"/"+current_date.getFullYear()+" "+current_date.getHours()+":"+current_date.getMinutes();
	
	parse_wage('');
}	

var item_names = new Array();
var item_quanity = new Array();
function acp_add_item(hidden_field,table,item_id,quanity)
{
	var quan = parse_input_number(document.getElementById(quanity).value);
	var item = parse_input_number(document.getElementById(item_id).value);
	if(quan == 0 || !(item in item_names))
	{
		return;
	}
	
	acp_compile_quanity(hidden_field);
	
	if(!(item in item_quanity))
	{
		item_quanity[item] = 0;
	}
	item_quanity[item] += quan;
	if(item_quanity[item] <= 0)
	{
		delete(item_quanity[item]);
	}
	
	acp_save_quanity(hidden_field);
	acp_display_item_table(table,hidden_field);
}

function acp_delete_item(item, table, hidden_field)
{
	acp_compile_quanity(hidden_field);
	delete(item_quanity[item]);
	acp_save_quanity(hidden_field);
	acp_display_item_table(table,hidden_field);
	return false;
}

function acp_compile_quanity(hidden_field)
{
	item_quanity = new Array();
	field_value = document.getElementById(hidden_field).value;
	
	if(field_value == "")
	{
		return;
	}
	
	var items = field_value.split(",");
	for(var i in items)
	{
		var temp = items[i].split("=");
		var item_id = parseInt(temp[0]);
		var quan = parseInt(temp[1]);
		if(item_id in item_names)
		{
			item_quanity[item_id] = quan;
		}
	}
}

function acp_save_quanity(hidden_field)
{
	var items = new Array();
	for(var id in item_quanity)
	{
		items[items.length] = id+"="+item_quanity[id];
	}
	
	document.getElementById(hidden_field).value = items.join(",");
	item_quanity = new Array();
}

function acp_display_item_table(table, hidden_field)
{
	var tableobj = document.getElementById(table);
	while(tableobj.rows.length > 0)
	{
		tableobj.deleteRow(tableobj.rows.length - 1);
	}
	
	acp_compile_quanity(hidden_field);
	
	var newRow, newCell;
	for(var id in item_quanity)
	{
		newRow = tableobj.insertRow(tableobj.rows.length);
		newCell = newRow.insertCell(0);
		newCell.innerHTML = "<b>"+item_names[id]+"</b>";
		newCell.className = 'tablerow1';
		newCell = newRow.insertCell(1);
		newCell.innerHTML = item_quanity[id];
		newCell.className = 'tablerow2';
		newCell = newRow.insertCell(2);
		newCell.innerHTML = "<a href='#' title='Remove Item' style='color:red;font-weight:bold' onclick=\"return acp_delete_item('"+id+"','"+table+"','"+hidden_field+"')\">[X]</a>";
		newCell.className = 'tablerow3';
	}
}

/*--------------------------------------------*/
// Generic code to be used anywhere
/*--------------------------------------------*/

function _get_time_now()
{
	var mydate = new Date();
	return mydate.getTime();
}

function format_number(number,include_currency)
{
	number = number + "";
	var a = number.split('.',2);
	var d = parse_input_number(a[1]);
	var i = parse_input_number(a[0]);
	var minus = '';
	if(i < 0)
	{
		minus = '-';
	}
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0)
	{
		a.unshift(n);
	}
	n = a.join(comma_space);
	if(d == 0)
	{
		amount = n;
	}
	else
	{
		amount = n + decimal_space + d;
	}
	if(include_currency)
	{
		amount = currecy_prefix + amount + currecy_suffix;
	}
	amount = minus + amount;
	return amount;
}

function check_24_time(month, day, year, hour, minute)
{
	var check = true;
	check = check && (month <= 12 && month >= 1);
	check = check && year >= 1970;
	
	if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
	{
		check = check && (day <= 31 && day >= 1);
	}
	else if(month == 2)
	{
		if((year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
		{
			check = check && (day <= 29 && day >= 1);
		}
		else
		{
			check = check && (day <= 28 && day >= 1);
		}
	}
	else
	{
		check = check && (day <= 30 && day >= 1);
	}
	
	check = check && hour >= 0 && hour < 24;
	check = check && minute >= 0 && minute < 59;
	
	return check;
}

// Very similar to intval() in PHP
function parse_input_number(number)
{
	var n = parseInt((number+"").replace(/^0+/,''));
	n = isNaN(n) ? 0 : n;
	
	return n;
}

function form_change_ribbon(icon,id)
{
	document.images[eval("'ribbon_"+id+"_preview'")].src = icon_url + '/ribbons/' + icon;
}

